< previous page page_381 next page >

Page 381
LISTING 17.2. CODE THAT READS A FILE ONE LINE AT A TIME.

9f8044bafea36a1f728739c0bb58ef4c.gif
1: Dim vFileContents, sCurrentLine As String
2: Do Until EOF(iFileNum)
3:     Line Input #iFileNumber, NextLine
4:     vFileContents = vFileContents + sCurrentLine + vbNewLine
5: Loop
6:

Notice the use of the Visual Basic constant vbNewLine. The Line Input # statement uses this to identify the end of a line and the beginning of the next. However, the value of vbNewLine is ignored when values are copied from the line in the file to your variables. Thus, in those situations where you want the vbNewLine value, you need to explicitly add it to your variables in code, not in the file. The Input # statement by itself is very useful. If you have a number of variables into which you want to read values from a sequential file, use a syntax like
Input #iFileNumber, sLastName, sFirstName, sBankTellerID, sWorkgroup, sRole
The Input function, without the pound sign (#), can copy from one character to the entire contents of a file into a variable large enough to handle the information. The Text property of the Rich Text Box control can certainly handle large amounts of information. String variables also are large enough, but be careful about eating up your computer's memory. To read the entire contents of a file into a variable, use the following syntax:
vFileContents = Input(LOF(iFileNumber), iFileNumber)
Persisting Information to a Sequential Access File
Use the Print # statement to store information in your application's variables into a sequential access file. Having a class named SequentialFile with a method called something like persistNewInfo(argFileName) might help. The syntax for persisting information is as follows:
Print #iFileNumber, sLotsOfInfo
If you have information that needs to be read from variables of different data types and later needs to be exported to a database such as Access, you might choose the Write # statement as an alternative to Print #. The Write # statement writes (or persists) a list of string or numeric values to a given file. The beauty of the Write # statement is that it

 
< previous page page_381 next page >

If you like this book, buy it!